
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;

public class TaskEditor extends ItemEditor<Task>
{
    public TaskEditor(Judicator k, Shell parent, String title, Task t)
    {
        super(k,parent,title,t,360,110);
    }

    Label nameLabel, pointsLabel;
    Text name, points;

    private static final String
        NO_NAME = "Name required.",
        NO_PTS = "Point number required.",
        BAD_NUM = " is not a valid number.";

    protected void _init()
    {
        nameLabel = new Label(shell, SWT.NATIVE);
        pointsLabel = new Label(shell, SWT.NATIVE);
        name = new Text(shell, SWT.BORDER);
        points = new Text(shell, SWT.BORDER);

        nameLabel.setText("name:");
        nameLabel.setBounds(20,30,40,30);

        pointsLabel.setText("points:");
        pointsLabel.setBounds(20,70,40,30);

        name.setBounds(60,30,250,20);
        name.setEditable(true);

        points.setBounds(60,70,250,20);
        points.setEditable(true);

        name.setFocus();
    }

    protected void _assumeData()
    {
        name.setText(target.name);
        points.setText(""+target.value);
    }

    protected void _addItem()
    {
        if (points.getText().length() < 1)
        {
            AlertSender.sendAlert(NO_PTS);
            return;
        }
        if (name.getText().length() < 1)
        {
            AlertSender.sendAlert(NO_NAME);
            return;
        }

        boolean numberOkay = true;
        Task t = new Task();
        try
        {
            t.name = name.getText();
            t.value = Double.parseDouble(points.getText());
        }
        catch (NumberFormatException e)
        {
            numberOkay = false;
            AlertSender.sendAlert(points.getText() + BAD_NUM);
        }
        if (numberOkay)
        {
            judicator.addTask(t);
            close();
        }
    }

    protected void _modifyItem()
    {
        if (points.getText().length() < 1)
        {
            AlertSender.sendAlert(NO_PTS);
            return;
        }
        if (name.getText().length() < 1)
        {
            AlertSender.sendAlert(NO_NAME);
            return;
        }

        boolean numberOkay = true;
        try
        {
            target.name = name.getText();
            target.value = Double.parseDouble(points.getText());
        }
        catch (NumberFormatException e)
        {
            numberOkay = false;
            AlertSender.sendAlert(points.getText() + BAD_NUM);
        }
        if (numberOkay)
        {
            judicator.update(target);
            close();
        }
    }

    protected void _close()
    {
        judicator.mainWindow.taskEditor = null;
    }
}
